################################################################################
# Created By:Pietryka
# Creation Date:  2016-08-22
# Purpose: Matching estimates for the CPS turnout data
# Questions: mpietryka@fsu.edu
################################################################################


# PREAMBLE  ====================================



# LOAD PACKAGES --------------

library(dplyr)
library(purrr)
library(cem)



# LOAD DATA --------------

# created in 'CPS-2A-Impute-Turnout.R'
load("Data/CPS-2A-Impute-Turnout.RData")



# COARSENED EXACT MATCHING   ====================================




# SPECIFY CUTS  --------------
age_cuts <- c(18, seq(20, 85, 5))
income_cuts <- c(1, 4, 7, 9, 11, 12, 13)
dummy_cuts <- seq(0, 1, .1)


turnout_cuts <- lapply(covariates, function(x){
  x = dummy_cuts
})
names(turnout_cuts) <- covariates
turnout_cuts$age <- age_cuts
turnout_cuts$famincome <- income_cuts


turnout_cuts <- list(
  educ = c(31, 39, 40, 43, 46),
  age = c(18, seq(20, 85, 5)),
  PES8 = c(1, 4, 6)

)
turnout_groups <- list(
  famincome = c(1:15),
  female = c(0:1),
  student = c(0:1),
  PEMARITL = list(1, c(2:5), 6),
  PTDTRACE = list(1, 2, c(3:6)),
  PEMLR = list(1, 5, c(2:4, 6:7))
)


impute_10_df <- map(impute_10, data.frame)
impute_14_df <- map(impute_14, data.frame)

# 2010
turnout_cem_10 <- cem(
  treatment = "prekkids",
  datalist =  impute_10_df,
  data = data.frame(cps_sub_10),
  drop = "turnout",
  eval.imbalance = FALSE,
  cutpoints = turnout_cuts,
  grouping = turnout_groups
  )

# 2014
turnout_cem_14 <- cem(
  treatment = "prekkids",
  datalist =  impute_14_df,
  data = data.frame(cps_sub_14),
  drop = "turnout",
  eval.imbalance = FALSE,
  cutpoints = turnout_cuts,
  grouping = turnout_groups
  )


# ESTIMATES ===================================================

# Formulae
model_form_int <- formula(turnout ~ prekkids*female  + age + famincome)
model_form     <- formula(turnout ~ prekkids  + age + famincome)
model_form     <- formula(turnout ~ prekkids)




# 2010
turnout_est_10 <- att(turnout_cem_10,
                model_form,
                model = "lm",
                data = impute_10_df)
turnout_est_10
t(turnout_est_10$att.model)

# 2014
turnout_est_14 <- att(turnout_cem_14,
                model_form,
                model = "lm",
                data = impute_14_df)
turnout_est_14
t(turnout_est_14$att.model)


# Gender Interaction  ---------------------------

# 2010
int_est_10 <- att(
  turnout_cem_10,
  model_form_int,
  model = "lm",
  data = impute_10_df
  )
int_est_10
t(int_est_10$att.model)


coef <- t(int_est_10$att.model)["prekkids:female", "Estimate"]
se   <- t(int_est_10$att.model)["prekkids:female", "Std. Error"]
coef - 1.96*se
coef + 1.96*se

# 2014
int_est_14 <- att(
  turnout_cem_14,
  model_form_int,
  model = "lm",
  data = impute_14_df
  )
int_est_14
t(int_est_14$att.model)


coef <- t(int_est_14$att.model)["prekkids:female", "Estimate"]
se <- t(int_est_14$att.model)["prekkids:female", "Std. Error"]
coef - 1.96*se
coef + 1.96*se


# Save =============================================
save.image("Data/CPS-4A-Matching-Turnout.Rdata")

beepr::beep(sound = 8)
